<template>
    <div>
        <div class="container">
            <div class="handle-box">
                <el-input v-model="query.keyword" placeholder="设备名称" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="func_handleSearch" plain>搜索</el-button>
                <el-button type="success" class="mr10" icon="el-icon-search" @click="func_handleAdd" plain>添加</el-button>
				<el-dropdown :hide-on-click="false">
                    <el-button class="green mr10" plain>操作</el-button>
					<template #dropdown>
						<el-dropdown-menu>
                            <el-dropdown-item type="text" @click="func_handleBatchDel" class="red "> 批量删除</el-dropdown-item>
						</el-dropdown-menu>
					</template>
				</el-dropdown>
            </div>
            <el-table :data="memberDeviceTableData" border class="table" header-cell-class-name="table-header" @selection-change="func_handleSelectionChange">
                <el-table-column type="selection" width="55" />
                <el-table-column prop="id" label="ID" width="55" align="center" v-if="false" />
				<el-table-column label="序号" width="80" align="center"><template #default="scope">
						{{scope.$index + 1}}
				</template></el-table-column>
                <el-table-column prop="memberName" label="会员名称" />
				<el-table-column prop="devName" label="设备名称" />
                <el-table-column prop="devSno" label="设备SN码" />
				<el-table-column prop="devType" label="	设备型号" />
				<el-table-column prop="identificationCode" label="	识别码" />
                <el-table-column label="操作" width="180" align="center">
                    <template #default="scope">
                        <el-button type="text" icon="el-icon-edit" @click="func_handleEdit(scope.$index, scope.row)">编辑
                        </el-button>
						<el-button type="text" icon="el-icon-delete" class="red" @click="func_handleDelete(scope.$index, scope.row)">删除
						</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination background layout="total, prev, pager, next" :current-page="query.pageNum"
                    :page-size="query.pageSize" :total="dataTotal" @current-change="func_handlePageChange"></el-pagination>
            </div>
        </div>
        <!-- 编辑弹出框 -->
        <el-dialog title="编辑会员设备" v-model="editMemberDeviceDialogVisible" width="55%" :close-on-click-modal="false">
            <VMemberDevice v-bind:selectValue="VMemberDeviceData" @MemberDeviceData="func_loadVMemberDeviceData" />
        </el-dialog>
    </div>
</template>

<script>
import { ref, reactive, toRefs } from "vue";
import { useRouter, useRoute } from "vue-router";
import { ElMessage, ElMessageBox } from "element-plus";
import { findMemberDeviceListPage, deleteMemberDeviceById, batchDeleteMemberDevice } from "../../../api/hairmanager/memberDevice";
import { NO_DATA_SELECT_ERROR, SUCCESS_ADDDATA_DIALOG_MESSAGE, SUCCESS_DELDATA_DIALOG_MESSAGE, SUCCESS_EDITDATA_DIALOG_MESSAGE, formLabelWidth, } from "../../../api/CommonErrorMessage.js";
import VMemberDevice from "./components/memberDeviceForm.vue";

export default {
    name: "borderTable",
    components: {VMemberDevice, },
    setup() {
        const router = useRouter();
		const route = useRoute();
        const formLabelWidth = ref("170px");
        const optColumnWidth = 150;
        const query = reactive({
            keyword: "",
            pageNum: 1,
            pageSize: 10,
        });
        const memberDeviceTableData = ref([]);
        const dataTotal = ref(0);
        const multipleSelection = ref([]);
        const VMemberDeviceData = reactive({
            load: false,
            id: 0,
        });
        let idx = -1;
        // =========================================================================================================
        // 主业务处理 ========================================================================================= START
        /**
         * @name 主表格查询操作
         */
        const func_handleSearch = () => {
            query.pageNum = 1;
            func_getData();
        };
        /**
         * @name 分页导航
         */
        const func_handlePageChange = (val) => {
            query.pageNum = val;
            func_getData();
        };
        /**
         * @name 删除操作
         */
        const func_handleDelete = (index, row) => {
            // 二次确认删除
            ElMessageBox.confirm("确定要删除吗？", "提示", {type: "warning",}).then(() => {
                func_del(row['id']);
            }).catch(() => {});
        };
        /**
         * @name 添加数据
         */
        const func_handleAdd = () => {
            VMemberDeviceData['id'] = 0;
            VMemberDeviceData['load'] = false;
            editMemberDeviceDialogVisible.value = true;
        };
        /**
         * @name 行编辑操作
         */
        const func_handleEdit = (index, row) => {
            idx = index;
            VMemberDeviceData['id'] = row['id'];
            VMemberDeviceData['load'] = true;
            editMemberDeviceDialogVisible.value = true;
        };
        /**
         * @name 批次删除数据
         */
        const func_handleBatchDel = () => {
            if(multipleSelection.value.length ===0){
                ElMessage.warning(NO_DATA_SELECT_ERROR);
                return false;
            }
            // 二次确认删除
            ElMessageBox.confirm("确定要删除吗？", "提示", {type: "warning", }).then(() => {
                func_batchDel(multipleSelection.value);
            }).catch(() => {});
        }
        const func_handleSelectionChange = (val) => {
            let ids = [];
            val.forEach((element) => {
                ids.push(element['id']);
            });
            multipleSelection.value = ids;
        };
        // 主业务处理 ========================================================================================= END
/**
         * @name 删除广告位配置
         */
        const func_del = (id) => {
            deleteMemberDeviceById(id).then((res)=>{
                if(res.code != 200) {
                    if(res.type == 0) {
                        ElMessage.warning(res.message);
                    } else {
                        ElMessage.error(res.message);
                    }
                } else {
                    ElMessage.success(SUCCESS_DELDATA_DIALOG_MESSAGE);
                    func_getData(); // 刷新数据
                }
            });
        };
        /**
         * @name 批量删除广告位配置
         */
        const func_batchDel = (v) => {
            batchDeleteMemberDevice(obj).then((res) => {
                if(res.code != 200) {
                    if(res.type == 0) {
                        ElMessage.warning(res.message);
                    } else {
                        ElMessage.error(res.message);
                    }
                } else {
                    ElMessage.success(SUCCESS_EDITDATA_DIALOG_MESSAGE);
                    func_getData(); // 刷新数据
                }
            });
        }
        /**
         * @name 获取表格数据
         */
        const func_getData = () => {
            findMemberDeviceListPage(query).then((res) => {
                memberDeviceTableData.value = res.data.list;
                dataTotal.value = res.data.total;
            });
        };
        func_getData();
		// 扩展业务处理 ========================================================================================= START
		// 表格编辑时弹窗和保存
        const editMemberDeviceDialogVisible = ref(false);
        const func_loadVMemberDeviceData = (v) => {
            if(v['state'] === 200){
                if(v['editFlg']){
                    ElMessage.success(`修改第 ${idx + 1} 行成功`);
                }
                editMemberDeviceDialogVisible.value = false;
                VMemberDeviceData['load'] = null;
                func_getData();
            } else if(v['state'] === 1){
                editMemberDeviceDialogVisible.value = false;
                VMemberDeviceData['load'] = null;
                func_getData();
            } else {
                editMemberDeviceDialogVisible.value = true;
            }
        };
		// 扩展业务处理 ========================================================================================= END
        // =========================================================================================================
        return {
            // 系统变量 =========================================================================================
            optColumnWidth, formLabelWidth, query, memberDeviceTableData, dataTotal, multipleSelection, 
            // 主表格调用方法 =========================================================================================
            func_handleSearch, func_handlePageChange, func_handleDelete, func_handleAdd, func_handleEdit, func_handleBatchDel, func_handleSelectionChange,
            // 数据执行处理 =========================================================================================
            func_getData, func_del, func_batchDel,
            // 插件业务调用方法 =========================================================================================
			editMemberDeviceDialogVisible, VMemberDeviceData, func_loadVMemberDeviceData,
        };
    },
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>